﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace POS
{
    public partial class ServerCloseOutScreen : DevExpress.XtraEditors.XtraUserControl
    {
        double ServerReportedCashOnHand;
        double TotalInSales;
        int NumberofTickets = 0;

        double TaxCollected = 0;
        double AutoGratCollected = 0;
        double GratCollected = 0;

        double MoneyCollected = 0;

        double Payout = 0;

        double SalesCC = 0;
        double CCTips = 0;
        double SalesCASH = 0;
        double CASHTips = 0;
        double TotalTips = 0;


        double CalcCashOnHand = 0;
        double AmountClaimed = 0;
        double diffrence = 0;

        double ServerTakeHomeTotal = 0;

        public ServerCloseOutScreen()
        {
            InitializeComponent();

            foreach(Control item in this.Controls)
            {
                item.ForeColor = this.ForeColor;
            }
            lvTickets.BackColor = this.BackColor;

            comboBoxEdit1.Properties.Items.Clear();
            StaticProperties.GetEmployeeNameList();
            foreach (DataRow item in DataTableClass.EmployeeLogin.Rows)
            {
                if (item["EmployeeTitle"].ToString().Trim() == "SERVER")
                {
                    comboBoxEdit1.Properties.Items.Add(item["EmployeeFullName"].ToString().Trim());
                }
                else if (item["EmployeeTitle"].ToString().Trim() == "BAR TENDER")
                {
                    comboBoxEdit1.Properties.Items.Add(item["EmployeeFullName"].ToString().Trim());
                }
            }

            lblDate.Text = DateTime.Now.ToShortDateString();
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            labelControl2.Visible = false;
            textEdit1.Visible = false;
            simpleButton3.Visible = false;

            labelControl24.Visible = false;
            comboBoxEdit1.Visible = false;
            labelControl13.Visible = false;

            labelControl5.Visible = true;
            labelControl6.Visible = true;
            labelControl7.Visible = true;

            labelControl8.Visible = true;
            lblTotalSales.Visible = true;
            labelControl8.ForeColor = Color.SeaGreen;

            lblPayoutTotal.Visible = true;
            //lblNoTickets.Visible = true;
            lblServerTakeHome.Visible = true;
            lblServerTakeHome.ForeColor = Color.SeaGreen;

            labelControl9.Visible = true;
            labelControl11.Visible = true;
            labelControl12.Visible = true;
            labelControl14.Visible = true;

            lblTotalTaxCollected.Visible = true;
            lblTotalAutoGrats.Visible = true;
            lblTotalGrats.Visible = true;
            lblMoneyCollected.Visible = true;

            labelControl15.Visible = true;
            labelControl17.Visible = true;
            lblTotalSalesCC.Visible = true;
            lblTotalSalesCash.Visible = true;

            simpleButton1.Visible = true;
            simpleButton2.Visible = true;

            labelControl3.Visible = true;
            labelControl4.Visible = true;
            lblServerName.Visible = true;
            lblServerID.Visible = true;
            lblDate.Visible = true;

            lvTickets.Visible = true;

            labelControl2.Visible = true;
            lblNumberOfSales.Visible = true;

            lblCashTips.Visible = true;
            labelControl20.Visible = true;
            lblCCTips.Visible = true;
            labelControl23.Visible = true;

            labelControl19.Visible = true;
            lblMoneyClaimed.Visible = true;

            simpleButton2.Enabled = true;

            labelControl16.Visible = true;
            labelControl18.Visible = true;
            labelControl21.Visible = true;
            lblCalcCashOnHand.Visible = true;
            labelControl25.Visible = true;
            labelControl26.Visible = true;

            lblTipoutTotal.Visible = true;
            labelControl30.Visible = true;
            labelControl31.Visible = true;

            labelControl37.Visible = true;
            lblTotalTips.Visible = true;
            labelControl39.Visible = true;

            lblCashDiffrence.Visible = true;
            labelControl48.Visible = true;

            ServerReportedCashOnHand = Convert.ToDouble(textEdit1.Text);

            LoadShiftReport();
        }

        private void LoadShiftReport()
        {
            
            foreach(DataRow item in DataTableClass.EmployeeLogin.Rows)
            {
                if(item["EmployeeFullName"].ToString().Trim() == comboBoxEdit1.SelectedItem.ToString())
                {
                    StaticProperties.ServerName = item["EmployeeFullName"].ToString().Trim();
                    StaticProperties.ServerID = (int) item["EmployeeID"];

                    lblServerName.Text = StaticProperties.ServerName;
                    lblServerID.Text = StaticProperties.ServerID.ToString();
                    lblDate.Text = DateTime.Now.ToLongDateString();

                    foreach(DataRow item1 in DataTableClass.Receipt.Rows)
                    {
                        if (item1["ReceiptServerID"].ToString().Trim() == StaticProperties.ServerID.ToString())
                        {
                            if (DateTime.Parse(item1["ReceiptDate"].ToString().Trim()) == DateTime.Parse("02/07/2015"))
                            {
                                ListViewItem newList = new ListViewItem(item1["ReceiptNo"].ToString().Trim());
                                newList.SubItems.Add(item1["ReceiptSubTotal"].ToString().Trim());

                                double grat = Convert.ToDouble(item1["ReceiptGrat"]);
                                //double AutoGrat = Convert.ToDouble(item1["ReceiptAutoGrat"]);

                                double totalGrat = grat;// +AutoGrat;

                                newList.SubItems.Add(totalGrat.ToString("C"));
                                newList.SubItems.Add(item1["ReceiptPaymentType"].ToString().Trim());

                                lvTickets.Items.Add(newList);
                                TotalInSales += Convert.ToDouble(item1["ReceiptSubTotal"]);
                                TaxCollected += Convert.ToDouble(item1["ReceiptTaxTotal"]);
                                AutoGratCollected += Convert.ToDouble(item1["ReceiptAutoGrat"]);
                                GratCollected += Convert.ToDouble(item1["ReceiptGrat"]);

                                if (item1["ReceiptPaymentType"].ToString().Trim() == "CC")
                                {
                                    SalesCC += Convert.ToDouble(item1["ReceiptSubTotal"]);
                                    CCTips += Convert.ToDouble(item1["ReceiptGrat"]) + Convert.ToDouble(item1["ReceiptAutoGrat"]);
                                }
                                else if (item1["ReceiptPaymentType"].ToString().Trim() == "CASH")
                                {
                                    SalesCASH += Convert.ToDouble(item1["ReceiptSubTotal"]);
                                    CASHTips += Convert.ToDouble(item1["ReceiptGrat"]) + Convert.ToDouble(item1["ReceiptAutoGrat"]);
                                }

                                NumberofTickets++;

                            }
                        }
                    }

                    MoneyCollected = TotalInSales + GratCollected + AutoGratCollected;
                    TotalTips = CCTips + CASHTips;

                    AmountClaimed = Convert.ToDouble(textEdit1.Text);
                    lblMoneyClaimed.Text = String.Format("{0:C}", Convert.ToInt32(AmountClaimed));

                    lblNumberOfSales.Text = NumberofTickets.ToString();
                    lblTotalSales.Text = String.Format("{0:C}", Convert.ToInt32(TotalInSales)); 
                    lblTotalTaxCollected.Text = String.Format("{0:C}", Convert.ToInt32(TaxCollected)); 
                    lblTotalAutoGrats.Text = String.Format("{0:C}", Convert.ToInt32(AutoGratCollected)); 
                    lblTotalGrats.Text = String.Format("{0:C}", Convert.ToInt32(GratCollected)); 
                    lblMoneyCollected.Text = String.Format("{0:C}", Convert.ToInt32(MoneyCollected)); 

                    lblTotalSalesCC.Text = String.Format("{0:C}", Convert.ToInt32(SalesCC)); 
                    lblTotalSalesCash.Text = String.Format("{0:C}", Convert.ToInt32(SalesCASH)); 
                    lblCCTips.Text = String.Format("{0:C}", Convert.ToInt32(CCTips)); 
                    lblCashTips.Text = String.Format("{0:C}", Convert.ToInt32(CASHTips)); 
 
                    Payout = TotalInSales * 0.05;
                    lblPayoutTotal.Text = String.Format("{0:C}", Convert.ToInt32(Payout));

                    lblTotalTips.Text = String.Format("{0:C}", Convert.ToInt32(TotalTips)); 

                    lblTipoutTotal.Text = lblPayoutTotal.Text;

                    CalcCashOnHand = SalesCASH + CASHTips + TaxCollected;
                    lblCalcCashOnHand.Text = String.Format("{0:C}", Convert.ToInt32(CalcCashOnHand));
                    diffrence = CalcCashOnHand - AmountClaimed;

                    if (Convert.ToInt32(diffrence) == 0)
                    {
                        lblCashDiffrence.Text = String.Format("{0:C}", Convert.ToInt32(diffrence));
                    }
                    else
                    {
                        lblCashDiffrence.ForeColor = Color.Red;
                        lblCashDiffrence.Appearance.FontStyleDelta = FontStyle.Bold;
                        lblCashDiffrence.Text = String.Format("{0:C}", Convert.ToInt32(diffrence));
                    }

                    ServerTakeHomeTotal = TotalTips - Payout;
                    lblServerTakeHome.Text = String.Format("{0:C}", Convert.ToInt32(ServerTakeHomeTotal));



                }
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            MainProgram._Main.panelControl1.Controls.Clear();
            MainProgram._Main.panelControl1.Controls.Add(new HomeScreen());
        }
    }
}
